<?php

define("theme_url", get_template_directory_uri() . "/");
define('template', get_template_directory());
define('Module_template', get_template_directory() . '/Module-Template/');
define('img', get_template_directory_uri() . '/img/');
define('js', get_template_directory_uri() . '/js/');
define('css', get_template_directory_uri() . '/css/');
define('Content', get_template_directory() . '/Module-Template/Content/');
define('Footer', get_template_directory() . '/Module-Template/Footer/');
define('Header', get_template_directory() . '/Module-Template/Header/');

function get_script($name){
    if(!empty($name)):
        $name = "-{$name}";
    else:
        $name = "";
    endif;
    require(get_template_directory().'/script'.$name.'.php');
}

function get_header_form($name){
    if(!empty($name)):
        $name = "-{$name}";
    else:
        $name = "";
    endif;
    require(get_template_directory().'/Template/header/header-form'.$name.'.php');
}

/* Add Nav Menu */

register_nav_menus(array(
    'primary_navigation' => __('Primary Navigation'),
    'utility_navigation' => __('Utility Navigation')
));

/* Custom post type */

register_post_type(
        'project', array(
    'labels' => array(
        'name' => 'Project',
        'singular_name' => 'project'
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'project'),
    'supports' => array('title', 'editor'),
    'can_export' => true,
        )
);

register_taxonomy(
        'skills', 'project', array(
    'label' => __('Skills'),
    'rewrite' => array('slug' => 'skills'),
    'hierarchical' => true
        )
);

register_taxonomy(
        'abilities', 'project', array(
    'label' => __('Abilities'),
    'rewrite' => array('slug' => 'abilities'),
    'hierarchical' => true
        )
);

register_post_type(
        'ourteam', array(
    'labels' => array(
        'name' => 'Our Team',
        'singular_name' => 'ourteam'
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'ourteam'),
    'supports' => array('title', 'editor'),
    'can_export' => true,
        )
);

register_taxonomy(
        'position', 'ourteam', array(
    'label' => __('Position'),
    'rewrite' => array('slug' => 'position'),
    'hierarchical' => true
        )
);

add_action('category_edit_form_fields', 'editfields');

add_theme_support( 'post-thumbnails' );
add_image_size('related', 350 , 238, true);

function editfields() {
    echo "<tr class='form-field'><th>Form</th><td><textarea></textarea></td></tr>";
}

/* create post type widget */

register_post_type(
        'widgets', array(
    'labels' => array(
        'name' => 'Widgets',
        'singular_name' => 'widgets'
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'widgets'),
    'supports' => array('title'),
    'can_export' => true,
        )
);



// create widget areas: sidebar, footer // default Template
$sidebars = array('Sidebar');
foreach ($sidebars as $sidebar) {
    register_sidebar(array('name' => $sidebar,
        'before_widget' => '<div id="%1$s" class="widget %2$s border"><div class="sidebar-section twelve columns">',
        'after_widget' => '</div></div>',
        'before_title' => '<h4 class="title-widget">',
        'after_title' => '</h4>'
    ));
}

$sidebars = array('Footer');
foreach ($sidebars as $sidebar) {
    register_sidebar(array('name' => $sidebar,
        'before_widget' => '<div id="%1$s" class="span3 columns widget %2$s"><div class="footer-section">',
        'after_widget' => '</div></div>',
        'before_title' => '<h4>',
        'after_title' => '</h4>'
    ));
}

/* Custome Widget */

$get_widget = new WP_Query("post_type=widgets");
while ($get_widget->have_posts()): $get_widget->the_post();

    register_sidebar(array('name' => get_the_title(),
        'before_widget' => '<article id="%1$s" class="row widget %2$s"><div class="sidebar-section twelve columns">',
        'after_widget' => '</div></article>',
        'before_title' => '<h4>',
        'after_title' => '</h4>',
        'description' => get_the_content()
    ));
endwhile;

function Theme_enqueue_js() {
    /* wp_register_script( 'foundation-off-canvas', get_template_directory_uri() . '/javascripts/foundation/off-canvas.js' ); */
    //wp_enqueue_script( 'foundation-off-canvas', 'jQuery', '1.0', true );
}

add_action('wp_enqueue_scripts', 'Theme_enqueue_js');

function getNavMenu($menuName, $dropDownOption = "click") {
    $menu = $menuName; //Nav menu name
    $items = wp_get_nav_menu_items($menu); // Get nav menu items list
    $level = 0;
    $last_title = "";
    $last_url = "";
    $objectID_stack = array();
    $objectIDStackTop = 0;
    $output = "";
    $active = False;
    global $post;
    foreach ($items as $list) {
        if ($list->menu_item_parent == "0") {
            while (count($objectID_stack)) {
                array_pop($objectID_stack);
            }
            if ($level == 1) {
                if ($active)
                    $class = " class=\"active\"";
                else
                    $class = "";
                $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li>";
            }
            if ($level == 3) {
                if ($active)
                    $class = " class=\"active\"";
                else
                    $class = "";
                $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li></ul></li></ul></li>";
            }
            if ($level == 2) {
                if ($active)
                    $class = " class=\"active\"";
                else
                    $class = "";
                $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li></ul></li>";
            }
            $level = 1;
            array_push($objectID_stack, $list->object_id);
            $last_title = $list->title;
            $last_url = $list->url;
            if ($post->ID == $list->object_id)
                $active = True;
            else
                $active = False;
        }
        else {
            $stackTop = count($objectID_stack) - 1;
            if ($list->menu_item_parent == $objectID_stack[$stackTop]) {
                if ($level == 1) {
                    if ($active)
                        $class = " active";
                    else
                        $class = "";
                    if ($dropDownOption == "click")
                        $output = $output . "<li class=\"dropdown" . $class . "\"><a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">" . $last_title . "<b class=\"caret\"></b></a><ul class=\"dropdown-menu\">";
                    else
                        $output = $output . "<li class=\"dropdown" . $class . "\"><a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"" . $last_url . "\">" . $last_title . "<b class=\"caret\"></b></a><ul class=\"dropdown-menu\">";
                    $last_title = $list->title;
                    $last_url = $list->url;
                    if ($post->ID == $list->object_id)
                        $active = True;
                    else
                        $active = False;
                    $level = 2;
                    array_push($objectID_stack, $list->object_id);
                }
                else if ($level == 2) {
                    if ($active)
                        $class = " active";
                    else
                        $class = "";
                    if ($dropDownOption == "click")
                        $output = $output . "<li class=\"dropdown-submenu" . $class . "\"><a href=\"#\">" . $last_title . "</a><ul class=\"dropdown-menu sub-menu\">";
                    else
                        $output = $output . "<li class=\"dropdown-submenu" . $class . "\"><a href=\"" . $last_url . "\">" . $last_title . "</a><ul class=\"dropdown-menu sub-menu\">";
                    $last_title = $list->title;
                    $last_url = $list->url;
                    if ($post->ID == $list->object_id)
                        $active = True;
                    else
                        $active = False;
                    $level = 3;
                }
                else if ($level == 3) {
                    if ($active)
                        $class = " class=\"active\"";
                    else
                        $class = "";
                    $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li>";
                    $last_title = $list->title;
                    $last_url = $list->url;
                    if ($post->ID == $list->object_id)
                        $active = True;
                    else
                        $active = False;
                    $level = 3;
                }
            }
            else {
                if ($level == 2) {
                    if ($active)
                        $class = " class=\"active\"";
                    else
                        $class = "";
                    array_pop($objectID_stack);
                    $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li>";
                    $last_title = $list->title;
                    $last_url = $list->url;
                    if ($post->ID == $list->object_id)
                        $active = True;
                    else
                        $active = False;
                    $level = 2;
                    array_push($objectID_stack, $list->object_id);
                }
                if ($level == 3) {
                    if ($active)
                        $class = " class=\"active\"";
                    else
                        $class = "";
                    array_pop($objectID_stack);
                    $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li></ul></li>";
                    $last_title = $list->title;
                    $last_url = $list->url;
                    if ($post->ID == $list->object_id)
                        $active = True;
                    else
                        $active = False;
                    $level = 2;
                    array_push($objectID_stack, $list->object_id);
                }
            }
        }
    }
    if ($active)
        $class = " class=\"active\"";
    else
        $class = "";
    if ($level == 1) //If is parent and not printed.
        $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li>";
    else if ($level == 2) //If is sub and not printed.
        $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li></ul></li>";
    else if ($level == 3) //If is sub of sub and not printed.
        $output = $output . "<li" . $class . "><a href=\"" . $last_url . "\">" . $last_title . "</a></li></ul></li></ul></li>";
    return $output;
}


function et_custom_comments_display($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<article id="comment-<?php comment_ID(); ?>" class="comment-body clearfix">
			<?php echo get_avatar($comment,$size='60'); ?>
			
			<?php printf('<span class="fn">%s</span>', get_comment_author_link()) ?>
			<span class="comment_date">
				<?php
					/* translators: 1: date, 2: time */
					printf( __( '%1$s', 'Flexible' ), get_comment_date() ); 
				?>
			</span>		
			<?php edit_comment_link( esc_html__( '(Edit)', 'Flexible' ), ' ' ); ?>			

			<?php if ($comment->comment_approved == '0') : ?>
				<em class="moderation"><?php esc_html_e('Your comment is awaiting moderation.','Flexible') ?></em>
				<br />
			<?php endif; ?>
															
			<div class="comment-content clearfix">
				<?php comment_text() ?>
				
				<?php 
					$et_comment_reply_link = get_comment_reply_link( array_merge( $args, array('reply_text' => esc_attr__('Reply','Flexible'),'depth' => $depth, 'max_depth' => $args['max_depth'])) );
					if ( $et_comment_reply_link ) echo '<div class="reply-container">' . $et_comment_reply_link . '</div>';
				?>
			</div> <!-- end comment-content-->
		</article> <!-- end comment-body -->
<?php 

}